package com.msh.artascope.devcloud.service.config;

import com.msh.artascope.devcloud.service.interfaces.IGitAccess;
import com.msh.artascope.devcloud.service.service.*;
import com.msh.artascope.devcloud.service.thread.JenkinsBuildResultRunnable;
import com.msh.artascope.devcloud.service.wrapper.JenkinsWrapper;
import com.msh.frame.client.interfaces.IClassLoadLog;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;


/**
 * @author shihu
 * @mail m-sh@qq.com
 */
@Slf4j
@Configuration
public class BeanConfigure implements IClassLoadLog {
  private boolean jenkinsBuildResultRunnableLoad;


  @Bean
  public IGitAccess gitAccess(){
    return new IGitAccess() {
      @Override
      public boolean access(Long gitId) {
        return true;
      }
    };
  }


  @Bean
	@ConditionalOnProperty(value = "devcloud.bean.jenkins-build-result-runnable.enable", havingValue = "true", matchIfMissing = true)
  @SuppressWarnings("AlibabaAvoidManuallyCreateThread")
  public JenkinsBuildResultRunnable jenkinsBuildResultRunnable(JenkinsWrapper jenkinsWrapper, ModuleBuildService moduleBuildService, ProjectService projectService, EnvService envService, JenkinsConfigService jenkinsConfigService){
    JenkinsBuildResultRunnable jenkinsBuildResultRunnable = new JenkinsBuildResultRunnable(jenkinsWrapper, moduleBuildService, projectService, jenkinsConfigService, envService);
    jenkinsBuildResultRunnableLoad = true;
    new Thread(null,
        jenkinsBuildResultRunnable,
        "查询jenkins构建结果线程"
    ).start();
    return jenkinsBuildResultRunnable;
  }


  @Override
  public String logInfo() {
    StringBuilder sb = new StringBuilder().append("----执行类 ")
        .append(BeanConfigure.class)
        .append("\r\n");
    sb.append("--------加载类 ")
        .append(IGitAccess.class)
        .append("(gitAccess)")
        .append("\r\n");
    if (jenkinsBuildResultRunnableLoad) {
      sb.append("--------加载类 ")
          .append(JenkinsBuildResultRunnable.class)
          .append("(jenkinsBuildResultRunnable)")
          .append("\r\n");
    }
    return sb.toString();
  }
}
